<?php

/**
 * This is the model class for table "{{user}}".
 */
class User extends CActiveRecord
{
	/**
	 * The followings are the available columns in table '{{user}}':
	 * @var integer $user_id
	 * @var string $user_name
	 * @var string $password
	 * @var string $email
	 * @var integer $is_active
	 * @var integer $user_level
	 * @var string $avatar
	 * @var string $user_type
	 * @var integer $upper_num
	 * @var integer $fans_num
	 * @var integer $share_num
	 * @var integer $issue_num
	 * @var integer $is_remember
	 */
	public $repassword;
	public $favorLinks; //喜欢的链接数组
	public $upperType; //上游类型 user or recommend
	public $oldPassword;
	public $newPassword;
	public $cPassword;
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('password, email,repassword', 'required','on'=>'login,register,resetpasswd'),
			array('email','email','on'=>'login,register,forget,setting'),
			array('email','authenticate', 'skipOnError'=>true,'on'=>'login,register,forget,setting'),
			array('repassword','compare','compareAttribute'=>'password','on'=>'register,forget,resetpasswd'),
			array('user_name','required','on'=>'register'),
			array('password,oldPassword,newPassword,repassword,cPassword','length','min'=>'6','max'=>'12','on'=>'login,register,forget,setting,resetpasswd'),
			array('oldPassword, newPassword','required','on'=>'setting'),
			array('oldPassword','checkOldPassword','on'=>'setting'),
			array('cPassword','compare','compareAttribute'=>'newPassword','on'=>'setting'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user_profile'=>array(self::HAS_ONE,'Profiles','user_id'),
			'favors'=>array(self::HAS_MANY,'favor_user','user_id'),
			//'user_point'=>array(),



		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'user_name' => '昵称',
			'password' => '密码',
			'repassword' => '确认密码',
			'cPassword' => '确认密码',
			'oldPassword'=>'原始密码',
			'newPassword'=>'新密码',
			'email' => '邮箱',
			'favor_name'=> '兴趣',
			'is_active' => 'Is Active',
			'user_level' => 'User Level',
			'avatar' => 'Avatar',
			'user_type' => 'User Type',
			'upper_num' => 'Upper Num',
			'fans_num' => 'Fans Num',
			'share_num' => 'Share Num',
			'issue_num' => 'Issue Num',
			'is_remember' => 'Is Remember',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);

		$criteria->compare('user_name',$this->user_name,true);

		$criteria->compare('password',$this->password,true);

		$criteria->compare('email',$this->email,true);

		$criteria->compare('is_active',$this->is_active);

		$criteria->compare('user_level',$this->user_level);

		$criteria->compare('avatar',$this->avatar,true);

		$criteria->compare('user_type',$this->user_type,true);

		$criteria->compare('upper_num',$this->upper_num);

		$criteria->compare('fans_num',$this->fans_num);

		$criteria->compare('share_num',$this->share_num);

		$criteria->compare('issue_num',$this->issue_num);

		$criteria->compare('is_remember',$this->is_remember);

		$criteria->compare('favor_name',$this->favor_name);
		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));

	}
	public function checkOldPassword($attribute,$params){
		if(self::model()->findByPk(Yii::app()->user->id,"password = '".md5($this->oldPassword)."'"))
			return true;
		else
			$this->addError('oldPassword','原始密码不匹配！');
	}
	private $_identity;
	public function authenticate($attribute,$params){
		//$this->_identity=new UserIdentity($this->username,$this->password);
		//if(!$this->_identity->authenticate())
		//dbg($attribute,true);
		$em = $this->model()->findByAttributes(array('email'=>$this->email));
		if($em)
			$this->addError('email','邮箱 已被注册！');
	}
	public function validatePassword($password){
		//return $this->hasPassword($password,$this->user_type)===$this->password;
		dbg($password);
	}

	public function getUserInfoByUid($user_id){
		return $this->model()->with(array(
			'user_profile'=>array(
				'select'=>'qq,web,twitter',
			),
		))->find('t.user_id=?',array($user_id));
	}
	public function beforeSave(){
		
		if(parent::beforeSave()){
			if($this->isNewRecord){ //是否最新记录
				 //是否已存在这个用户
				 //....
				$this->password = md5($this->password);
			}
			return true;
		}else{
			return false;
		}
	}
	public function afterSave(){
	}

	public function register($model,$arr){
		if($arr['password']==$arr['repassword']){
			parent::save();
		}
	}
	public function afterFind(){
		$this->setFavorLinks($this->favor_name);
		parent::afterFind();
	}
	public function getFavorLinks()
	{
		return $this->_favorLinks;
	}
	public function setFavorLinks($favorName){
		$links=array();
		foreach(NC::string2array($favorName) as $key => $tag){
			if($tag){
				//$links[]=CHtml::link(CHtml::encode($tag), array('/favor/index', 'tag'=>$tag));
				$links[] = CHtml::encode($tag);
			}
		}
		$this->favorLinks = $links;
	}
//	public function updatePassword($id,$attr){
//		$model=$this->loadModel($id);
//		
//	}
//	public function loadModel($id){
//		$model=User::model()->findByPk((int)$id);
//		if($model===null)
//			throw new CHttpException(404,'The requested page does not exist.');
//		return $model;
//	}
	//添加新闻时更新uer表
	public static function updateByNews($newsTag){
		$user = self::model()->findByPk(Yii::app()->user->id);
		$oldTag = $user->news_tag_name;
		if($oldTag){
			$oldTag = NC::string2array($oldTag);
		}else $oldTag = array();
		$newsTag = NC::string2array($newsTag);
		$nowTag = array_merge($oldTag,$newsTag);
		$nowTag = array_unique($nowTag);
		$nowTag = NC::array2string($nowTag);
		$share_num = $user->share_num+1;
		$issue_num = $user->issue_num+1;
		$r = self::model()->updateAll(
			array(
				'news_tag_name'=>$nowTag,
				'share_num'=>$issue_num,
				'issue_num'=>$issue_num,
			),
			'user_id='.Yii::app()->user->id
		);
		if($r)return true;
		return false;
	}
	public static function login($email,$password){
		$login = new AccountForm;
		$login->email = $email;
		$login->password = $password;
		return $login->login();
	}
}